//
//  IsLanDescribTabView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/3.
//  Copyright © 2016年 game. All rights reserved.
//

#import "IsLanDescribTabView.h"
#import "IsLanTabHead.h"
#import "LocalAndCallCell.h"
#import "IslanScetionHeard.h"
#import "JianjieCell.h"
#import "TupianCell.h"
#import "DianpingCell.h"
#import "DianPCellFoot.h"
#include "IslanDescribNearbyTableview.h"
#import "IslanDescribModel.h"
#import "islanDescribDiaochangModel.h"
#import "LoginController.h"
#define  footCellHeight (SCREEN_HEIGHT*0.13)

@interface IsLanDescribTabView ()<UITableViewDataSource,UITableViewDelegate,TupianCellMorelImage,IslanDescribNearbyTableviewDeleget,DianpingCellDeleget,MBProgressHUDDelegate>
@property (nonatomic,strong) IsLanTabHead *head;
@property (nonatomic,assign) CGFloat CallCellhight;
@property (nonatomic,assign) CGFloat JianjieCellhight;
@property (nonatomic,assign) CGFloat tupianCellhight;
@property (nonatomic,assign) CGFloat dianpingCellHight;
@property (nonatomic,strong) AppDelegate *app;
@property (nonatomic,strong) NSUserDefaults *Myud;
/**
 *  最后一个Section的Foot 显示周边钓场
 */
@property (nonatomic,strong) IslanDescribNearbyTableview *tableFooter;
/**
 *  点评Section的FootView 当没有评论的时候显示  发表第一条评论
 */
@property (nonatomic,strong) DianPCellFoot *dianpFoot;
@property (nonatomic,strong) IslanDescribModel *describModel;
@property (nonatomic,strong) MBProgressHUD *HUD;
@end

@implementation IsLanDescribTabView

-(IslanDescribNearbyTableview *)tableFooter{
    if (!_tableFooter) {
        _tableFooter = [[IslanDescribNearbyTableview alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.16*5) style:UITableViewStylePlain];
        
        _tableFooter.Maindelget = self;
        _tableFooter.scrollEnabled = NO;
    }

    return _tableFooter;
}


-(DianPCellFoot *)dianpFoot{
    if (!_dianpFoot) {
        _dianpFoot = [[NSBundle mainBundle]loadNibNamed:@"DianPCellFoot" owner:nil options:nil].lastObject;
        
        [_dianpFoot.button addTarget:self action:@selector(dianpingclick:) forControlEvents:UIControlEventTouchUpInside];
            _dianpFoot.frame = CGRectMake(0, 0, SCREEN_HEIGHT, SCREEN_HEIGHT*0.08);
    }
    return _dianpFoot;
}

-(void)dianpingclick:(UIButton *)btn{
    
    [self.Maindeleget ComentActionWhithText:btn.currentTitle AndModelAry:self.diaochangModel.comments];
}


-(void)AchiveData{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic addEntriesFromDictionary:@{
                                    @"version":@"4",
                                    @"module":self.module,
                                    @"action":@"detail",
                                    @"id":self.Id,
                                    @"latitude":self.latitude,
                                    @"longitude":self.longitude,
                                    @"city":[self.Myud valueForKey:@"cityPinY"],
                                    
                                    }];
    
    [[Manager sharedManager]ResponseWhithURL:islanDetails AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
//        NSLog(@"%@",responseObject[@"data"]);
        self.describModel = [[IslanDescribModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        
        self.diaochangModel = self.describModel.detail;
        
        self.tableFooter.height = footCellHeight*self.diaochangModel.nearby.count;
        self.tableFooter.listAry = self.diaochangModel.nearby;
        
  
        self.GuanzhuBtn.selected = self.diaochangModel.follow.integerValue;
        
        [UIView transitionWithView:self duration:0.7 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            
            [self reloadData];
            
        } completion:nil];
        
        self.head.model = self.describModel.detail;
        
         [MBProgressHUD hideHUDForView:self];
    }];

}

-(void)setId:(NSString *)Id{
    _Id = Id;
    
    [self AchiveData];
}


-(void)setUpLoadImageFinish:(NSString *)upLoadImageFinish{
    _upLoadImageFinish = upLoadImageFinish;
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self AchiveData];
        });
   
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        
        self.dataSource = self;
        self.delegate = self;
        self.Myud = [NSUserDefaults standardUserDefaults];
        self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        
        [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

        [self registerNib:[UINib nibWithNibName:@"LocalAndCallCell" bundle:nil] forCellReuseIdentifier:@"LocalCell"];
        [self registerNib:[UINib nibWithNibName:@"JianjieCell" bundle:nil] forCellReuseIdentifier:@"jianjieCell"];
        
        [self registerNib:[UINib nibWithNibName:@"TupianCell" bundle:nil] forCellReuseIdentifier:@"tupianCell"];
        
        [self registerNib:[UINib nibWithNibName:@"DianpingCell" bundle:nil] forCellReuseIdentifier:@"dianpingCell"];
        [self CreatHead];
        
        [self creatHuD:nil];
        
    }
    return self;
}

-(void)CreatHead{

    self.head = [[NSBundle mainBundle]loadNibNamed:@"IsLanTabHead" owner:nil options:nil].lastObject;
    self.head.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.15);
    self.head.model = self.describModel.detail;
    self.tableHeaderView = self.head;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    if (!self.describModel.detail) {
        return 0;
    }else{
        if (section==0) {
            return 2;
        }else if (section==1){
            
            return 2;
        }else if (section==2){//显示评论数量
            
            
            NSInteger count = self.diaochangModel.comments.count;
            if (count>3) {
                count = 3;
            }
            return count;
        }
        else
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
        return self.CallCellhight+1;
        
    }else if (indexPath.section==1){
    
        if (indexPath.row==0) {
            return self.JianjieCellhight+1;
        }else{
            return self.tupianCellhight+1;
        }
        
    }else if (indexPath.section==2){
        return self.dianpingCellHight;
    }
    
    else{
        return 44;
    }
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{

    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    if (section==3) {
        return footCellHeight*self.diaochangModel.nearby.count;
    } if (section==2) {
        return SCREEN_HEIGHT*0.09;
    } else
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if (!self.describModel.detail) {
        return nil;
    }else{
    
        if (section==3) {
            return self.tableFooter;
        }else if(section== 2){
            self.dianpFoot.commentCount = self.diaochangModel.comments.count;
            return self.dianpFoot;
        }else
            return nil;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section==0) {
        return 0;
    }else
        return SCREEN_HEIGHT*0.07;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    if (!self.describModel.detail) {
        return nil;
    }else{
        NSArray *ary;
        if (self.isYuJuDian) {
            ary  = @[@"简介",@"点评",@"周边渔具店"];
        }else{
            ary  = @[@"简介",@"点评",@"周边钓场"];
        }
        
        if (section==0) {
            return nil;
        }else{
            IslanScetionHeard *head = [[NSBundle mainBundle]loadNibNamed:@"IslanScetionHeard" owner:nil options:nil].lastObject;
            head.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.07);
            
            head.title.text = ary[section-1];
            if (section!=2) {
                head.jiantou.alpha = 0;
                head.count.alpha = 0;
            }else{
                head.count.text = [NSString stringWithFormat:@"(%lu条评论)",(unsigned long)self.diaochangModel.comments.count];
            }
                return head;
        }
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        tableView.separatorStyle = 0;
       if (indexPath.section==0) {
        LocalAndCallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocalCell"];
        if (indexPath.row==0) {//显示地址cell
            
            cell.textString= self.diaochangModel.location;
            cell.imgView.image = [UIImage imageNamed:@"dingwei"];
        }else{
            cell.imgView.image = [UIImage imageNamed:@"iconfont-telephone"];

            cell.textString = self.diaochangModel.phone;

        }
             self.CallCellhight = cell.cellHight;
        return cell;
           
       }else if (indexPath.section==1){
       
           if (indexPath.row==0) {
               JianjieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jianjieCell"];
               cell.labText = self.diaochangModel.desc;
               self.JianjieCellhight = cell.cellhight;
               return cell;
           }else{
           
               TupianCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tupianCell"];
               cell.imageAry = self.diaochangModel.imgs;
               
               cell.deleget = self;
               self.tupianCellhight = cell.cellhight;
               return cell;
           }
       
       } else if (indexPath.section==2) {//评论
       
           DianpingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dianpingCell"];
           cell.model = self.diaochangModel.comments[indexPath.row];
           cell.deleget = self;
           cell.cellRow = indexPath.row;
           self.dianpingCellHight = cell.CellHight;
           return cell;
       
       }else{
           
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.textLabel.text = @"哈哈";
            return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
        
        if (indexPath.row==0) {//点击详细地址
             LocalAndCallCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            
            [self.Maindeleget ClickDiaochangLocation:cell.textString AndName:(NSString *)self.diaochangModel.name AndChargetype:self.diaochangModel.charge_type];
        }
        
        if (indexPath.row==1) {//点击电话
            
            LocalAndCallCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"拨打电话" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            
            //1.切割字符串
            NSMutableString *str = [[NSMutableString alloc] initWithString:cell.text.text];
            NSArray *arr = [str componentsSeparatedByString:@"  "];
            
            for (int i = 0; i < arr.count; i++) {
                
                
                UIAlertAction *action = [UIAlertAction actionWithTitle:arr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",arr[i]]]];
                    
                }];
                
                [alertController addAction:action];
                
            }
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            
            [alertController addAction:action];
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
            
        }    }
    
    /**
     *  点击简介
     */
    if (indexPath.section==1) {
        if (indexPath.row==0) {
            JianjieCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            
            [self.Maindeleget ShowDescrible:cell.labText];
        }
    }
}

/**
 *  图片简介Cell 点击查看更多图片代理方法
 */
-(void)MoreImage:(NSArray *)imageAry{
    [self.Maindeleget MoreImageToShow:imageAry];
}


-(void)IsLanTabClickWhithDetail:(NSString *)detail AndLatitude:(NSString *)latitude AndLongitude:(NSString *)longitude AndLocaltion:(NSString *)localtion AndID:(NSString *)Id{

    [self.Maindeleget IsLanTabClickWhithDetail:detail AndLatitude:latitude AndLongitude:longitude AndLocaltion:localtion AndID:Id];

}


#pragma 点评cell代理方法

-(void)PramisClickWhithRow:(NSInteger)row AndIslogin:(BOOL)islogin{

    if (islogin) {
        
        IslandCommentModel *model = self.diaochangModel.comments[row];
        
        model.iszan = YES;
        
    }else{
    
        [self.Maindeleget GotoLogin];
    
    }
}


-(void)personSpaceClickWhithPersonId:(NSString *)personId{
    
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        [self.Maindeleget ShowPersonSpaceWhithId:personId];
    }else{
        [self.Maindeleget GotoLogin];
    
    }
}

/**
 创建加载视图HUD
 */
-(void)creatHuD:(NSString*)title
{
    [self.HUD removeFromSuperview];
    // 隐藏时候从父控件中移除
    self.HUD.removeFromSuperViewOnHide = YES;
    
    self.HUD=[[MBProgressHUD alloc] initWithView:self];
    
    self.HUD.color = [UIColor clearColor];//菊花背景颜色
    self.HUD.activityIndicatorColor = [UIColor lightGrayColor];//菊花颜色
    
    [self addSubview:self.HUD];
    
    self.HUD.labelText=title;
    self.HUD.delegate=self;
    [self.HUD show:YES];
    [self sendSubviewToBack:self.HUD];
}


@end